/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpdisenio2009.services;

import java.util.List;
import tpdisenio2009.model.beans.FantasyTeam;
import tpdisenio2009.model.beans.Score;
import tpdisenio2009.model.beans.SoccerPlayer;
import tpdisenio2009.model.beans.TournamentDate;
import tpdisenio2009.model.dao.ScoreDAO;

/**
 *
 * @author DeuterioX
 */
public class ScoreService {

    private final ScoreDAO scoreDAO;

    public ScoreService() {

        scoreDAO = new ScoreDAO();
    }

    public List<Score> getScoresByTeamAndDate( FantasyTeam team, TournamentDate date ) {
        return scoreDAO.getByFantasyTeamAndDate( team, date );
    }

    public Integer getTotalScore( Integer scoreId ) {
        Score score = scoreDAO.getById( scoreId );
        int totalScore = score.getScore();
        if( score.getTime() >= Score.MIN_TIME_PLAYED ) {
            totalScore += score.getFailedPenalties() * Score.FAILED_PENALTY;
            if ( score.getFigure() ) { totalScore += Score.FIGURE;  }
            if ( score.getYellowCard() ) { totalScore += totalScore + Score.YELLOW_CARD; }
            if ( score.getRedCard() ) { totalScore += Score.RED_CARD; }
            if ( score.getSoccerPlayer().getPosition().equals( SoccerPlayer.GOALKEEPER_POSITION ) ) {
                totalScore += ( score.getGoals() * Score.GOALKEEPER_GOAL ) + ( score.getReceivedGoals() * Score.RECEIVED_GOAL ) + ( score.getCatchedPenalties() * Score.CATCHED_PENALTY );
                if ( score.getReceivedGoals() == 0 ) { totalScore += Score.NOT_RECEIVED_GOALS; }
            } else if ( score.getSoccerPlayer().getPosition().equals( SoccerPlayer.DEFENDER_POSITION ) ) {
                totalScore += ( score.getGoals() * Score.DEFENDER_GOAL ) + ( score.getOwnGoals() * Score.OWN_GOAL );
            } else if ( score.getSoccerPlayer().getPosition().equals( SoccerPlayer.MIDFIELDER_POSITION ) ) {
                totalScore += ( score.getGoals() * Score.MIDFIELDER_GOAL ) + ( score.getOwnGoals() * Score.OWN_GOAL );
            } else if ( score.getSoccerPlayer().getPosition().equals( SoccerPlayer.FORWARD_POSITION ) ) {
                totalScore += ( score.getGoals() * Score.FORWARD_GOAL ) + ( score.getOwnGoals() * Score.OWN_GOAL );
            } else {
                //Si no tiene una posición asiganada retorna 0.
                return 0;
            }
        }
        return totalScore;
    }
}
